package com.example.zyfyptlzx729.model;

import com.example.zyfyptlzx729.iface.CollectListener;
import com.example.zyfyptlzx729.iface.Collectiface;
import com.example.zyfyptlzx729.service.CollectService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.example.zyfyptlzx729.common.Common.BASEURL;

public class CollectModel implements Collectiface {
    private Retrofit retrofit;

    public CollectModel()
    {//使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
    @Override
    public void collect(String mod, int id, String Sessionid, final CollectListener listener) {
        //使用Retrofit----2
        CollectService collectService=retrofit.create(CollectService.class);
        Call<String> call=collectService.collect(mod,id,Sessionid);
//使用Retrofit----3
        call.enqueue(new Callback<String>(){
            @Override
            public void onResponse(Call<String>call, Response<String> response){
                if(response.isSuccessful()&&response!=null)
                    {
                        if(response.body().trim().toString().equals("0"))
                            listener.onResponse("1");//收藏失败
                         else if(!response.body().trim().toString().contains("error"))
                                 listener.onResponse("2");//收藏成功
                                 else
                                 listener.onResponse("收藏："+response.body());
                     }
                else listener.onFail("collectonresponsefail");//
            }
            @Override
                    public void onFailure(Call<String>call,Throwable t){
                         listener.onFail("收藏失败："+t.toString());
                }
        });
    }

    @Override
    public void uncollect(String mod, int id, String Sessionid, final CollectListener listener) {
//使用Retrofit----2
        CollectService collectService=retrofit.create(CollectService.class);
        Call<String>call=collectService.uncollect(mod,id,Sessionid);
//使用Retrofit----3
        call.enqueue(new Callback<String>(){
            @Override
            public void onResponse(Call<String>call,Response<String>response){
                if(response.isSuccessful()&&response!=null)
                {
                    if(response.body().trim().toString().equals("0"))
                    listener.onResponse("3");//取消收藏失败
                    else if(response.body().trim().toString().equals("1"))
                    listener.onResponse("4");//取消收藏成功
                    else
                    listener.onResponse("取消收藏："+response.body());
                }
                else listener.onFail("uncollectonresponsefail");//uncollectonresponsefail
            }
            @Override
                    public void onFailure(Call<String>call,Throwable t){
                listener.onFail("取消收藏失败："+t.toString());
            }
        });
    }

    @Override
    public void exist(String mod, int id, String Sessionid, final CollectListener listener) {
        //使用Retrofit----2
        final CollectService collectService=retrofit.create(CollectService.class);
        Call<String>call=collectService.exists(mod,id,Sessionid);
//使用Retrofit----3
        call.enqueue(new Callback<String>(){
            @Override
            public void onResponse(Call<String>call,Response<String>response){
                if(response.isSuccessful()&&response!=null)
                {
                    if(response.body().trim().toString().equals("0"))//已收藏
                    listener.onResponse("5");
                    else if(response.body().trim().toString().equals("1"))//未收藏
                    listener.onResponse("6");
                    else
                    listener.onResponse("判断收藏："+response.body());
                }
                else listener.onFail("collect exist onresponse fail");//
            }
            @Override
                    public void onFailure(Call<String>call,Throwable t){
                listener.onFail("判断收藏失败："+t.toString());
            }
        });
    }

}
